﻿using System.Web.UI;
using DotNetNuke.Entities.Modules;
using DotNetNuke.UI.Containers;

namespace HeroldIT.Dnn.Modules
{
    /// <summary>
    /// Provides extension methods for the <see cref="PortalModuleBase"/> class.
    /// </summary>
    public static class PortalModuleBaseExtensions
    {
        /// <summary>
        /// Finds the container for a module.
        /// </summary>
        /// <param name="moduleControl">The module to find the container for.</param>
        /// <returns>The module's <see cref="Container"/> control if available; otherwise <c>null</c>.</returns>
        public static Container FindContainer(this PortalModuleBase moduleControl)
        {
            if (null == moduleControl)
                return null;

            if (!moduleControl.ModuleConfiguration.DisplayTitle)
                return null;

            Control control = moduleControl.Parent;
            while (null != control)
            {
                Container container = control as Container;
                if (null != container)
                    return container;

                control = control.Parent;
            }

            return null;
        }
    }
}
